package com.qiantu.phone.widget.btnview;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;

import java.util.ArrayList;
import java.util.List;

/**
 * 画虚线
 * Created by luzx on 2019/05/05.
 */
public class OneTwoThreeFourSixEightNineButtonView extends BaseBtnView {

    private static final int[] SELECTED_STATE_SET = {
            android.R.attr.state_selected
    };
    private static final int[] UNSELECTED_STATE_SET = {
            -android.R.attr.state_selected
    };
    private int strokeColor;
    private int strokeWidth;
    private int radius;
    private Paint paint;
    private RectF mRectF;
    private String oneText;
    private String twoText;
    private String threeText;
    private String fourText;
    private String fiveText;
    private String sixText;
    private String sevenText;
    private String eightText;
    private String nineText;
    private int textSize;
    private int subTextSize;
    private ColorStateList textColor;
    private TextPaint mTextPaint;
    private int shadowRadius1;
    private int shadowRadius2;
    private int shadowRadius3;
    private int shadowRadiusDy1;
    private int shadowRadiusDy2;
    private int shadowRadiusDy3;
    private int centerRadiusOffset;
    private int lineColor;
    private int deviceSolidBgColor;
    private int lineHeight;
    private RectF mLineRectF1;
    private RectF mLineRectF2;
    private RectF mLineRectF3;
    private RectF mLineRectF4;
    private int keyNum;
    private boolean isPanel;
    private int addWidth;
    private Path mPath1;
    private Path mPath2;
    private Path mPath3;
    private Path mPath4;
    private Path mPath5;
    private Path mPath6;
    private Path mPath7;
    private Path mPath8;
    private Path mPath9;
    private int centerRadius;
    private SparseArray<Boolean> selectedKey = new SparseArray<>();

    public OneTwoThreeFourSixEightNineButtonView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public OneTwoThreeFourSixEightNineButtonView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.OneTwoThreeFourSixEightNineButtonView);
        strokeColor = ta.getColor(R.styleable.OneTwoThreeFourSixEightNineButtonView_one_two_three_four_six_eight_nine_button_view_stroke_color, 0);
        strokeWidth = ta.getDimensionPixelSize(R.styleable.OneTwoThreeFourSixEightNineButtonView_one_two_three_four_six_eight_nine_button_view_stroke_width, 1);
        radius = ta.getDimensionPixelSize(R.styleable.OneTwoThreeFourSixEightNineButtonView_one_two_three_four_six_eight_nine_button_view_radius, 10);
        keyNum = ta.getInt(R.styleable.OneTwoThreeFourSixEightNineButtonView_one_two_three_four_six_eight_nine_button_view_key_num, 2);
        isPanel = ta.getBoolean(R.styleable.OneTwoThreeFourSixEightNineButtonView_one_two_three_four_six_eight_nine_button_view_is_panel, false);
        oneText = ta.getString(R.styleable.OneTwoThreeFourSixEightNineButtonView_one_two_three_four_six_eight_nine_button_view_one_text);
        twoText = ta.getString(R.styleable.OneTwoThreeFourSixEightNineButtonView_one_two_three_four_six_eight_nine_button_view_two_text);
        threeText = ta.getString(R.styleable.OneTwoThreeFourSixEightNineButtonView_one_two_three_four_six_eight_nine_button_view_three_text);
        fourText = ta.getString(R.styleable.OneTwoThreeFourSixEightNineButtonView_one_two_three_four_six_eight_nine_button_view_four_text);
        fiveText = ta.getString(R.styleable.OneTwoThreeFourSixEightNineButtonView_one_two_three_four_six_eight_nine_button_view_five_text);
        sixText = ta.getString(R.styleable.OneTwoThreeFourSixEightNineButtonView_one_two_three_four_six_eight_nine_button_view_six_text);
        sevenText = ta.getString(R.styleable.OneTwoThreeFourSixEightNineButtonView_one_two_three_four_six_eight_nine_button_view_seven_text);
        eightText = ta.getString(R.styleable.OneTwoThreeFourSixEightNineButtonView_one_two_three_four_six_eight_nine_button_view_eight_text);
        nineText = ta.getString(R.styleable.OneTwoThreeFourSixEightNineButtonView_one_two_three_four_six_eight_nine_button_view_nine_text);
        textSize = ta.getDimensionPixelSize(R.styleable.OneTwoThreeFourSixEightNineButtonView_one_two_three_four_six_eight_nine_button_view_text_size, getResources().getDimensionPixelSize(R.dimen.dp_18));
        subTextSize = ta.getDimensionPixelSize(R.styleable.OneTwoThreeFourSixEightNineButtonView_one_two_three_four_six_eight_nine_button_view_text_size_sub, getResources().getDimensionPixelSize(R.dimen.dp_10));
        textColor = ta.getColorStateList(R.styleable.OneTwoThreeFourSixEightNineButtonView_one_two_three_four_six_eight_nine_button_view_text_color);
        // 解析后释放资源
        ta.recycle();
        lineColor = AppApplication.getInstance().getThemeColor(R.attr.secondaryColor8);
        deviceSolidBgColor = AppApplication.getInstance().getThemeColor(R.attr.deviceSolidBgColor);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);

        mTextPaint = new TextPaint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(textSize);
        mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);

        mPath1 = new Path();
        mPath2 = new Path();
        mPath3 = new Path();
        mPath4 = new Path();
        mPath5 = new Path();
        mPath6 = new Path();
        mPath7 = new Path();
        mPath8 = new Path();
        mPath9 = new Path();
        mRectF = new RectF();
        if(keyNum == 2){
            mLineRectF1 = new RectF();
        }else if (keyNum == 3 || keyNum == 4) {
            mLineRectF1 = new RectF();
            mLineRectF2 = new RectF();
        } else if(keyNum == 6){
            mLineRectF1 = new RectF();
            mLineRectF2 = new RectF();
            mLineRectF3 = new RectF();
        }else if (keyNum == 8 || keyNum == 9) {
            mLineRectF1 = new RectF();
            mLineRectF2 = new RectF();
            mLineRectF3 = new RectF();
            mLineRectF4 = new RectF();
        }
        /*if(centerDrawable != null && centerDrawableWidth != 0 && centerDrawableHeight != 0){
            BitmapDrawable bd = (BitmapDrawable) centerDrawable;
            Bitmap newBmp = Bitmap.createScaledBitmap(bd.getBitmap(), centerDrawableWidth, centerDrawableHeight, true);
            centerDrawable = new BitmapDrawable(getResources(), newBmp);
        }*/
        centerRadiusOffset = getResources().getDimensionPixelSize(R.dimen.dp_4);
        centerRadius = radius - centerRadiusOffset;
        lineHeight = getResources().getDimensionPixelSize(R.dimen.dp_2);
        shadowRadius1 = getResources().getDimensionPixelSize(R.dimen.dp_52);
        shadowRadius2 = getResources().getDimensionPixelSize(R.dimen.dp_26);
        shadowRadius3 = getResources().getDimensionPixelSize(R.dimen.dp_13);
        shadowRadiusDy1 = getResources().getDimensionPixelSize(R.dimen.dp_26);
        shadowRadiusDy2 = getResources().getDimensionPixelSize(R.dimen.dp_11);
        shadowRadiusDy3 = getResources().getDimensionPixelSize(R.dimen.dp_4);
        addWidth = shadowRadius1 - strokeWidth;
        addWidth = addWidth < 0 ? 0 : addWidth;
    }

    public void setText(String text1, String text2) {
        this.oneText = text1;
        this.twoText = text2;
        invalidate();
    }

    public void setText(String text1, String text2, String text3) {
        this.oneText = text1;
        this.twoText = text2;
        this.threeText = text3;
        invalidate();
    }

    public void setText(String text1, String text2, String text3, String text4) {
        this.oneText = text1;
        this.twoText = text2;
        this.threeText = text3;
        this.fourText = text4;
        invalidate();
    }

    public void setText(String text1, String text2, String text3, String text4, String text5, String text6, String text7, String text8) {
        this.oneText = text1;
        this.twoText = text2;
        this.threeText = text3;
        this.fourText = text4;
        this.fiveText = text5;
        this.sixText = text6;
        this.sevenText = text7;
        this.eightText = text8;
        invalidate();
    }

    public void setText(String text1, String text2, String text3, String text4, String text5, String text6, String text7, String text8, String text9) {
        this.oneText = text1;
        this.twoText = text2;
        this.threeText = text3;
        this.fourText = text4;
        this.fiveText = text5;
        this.sixText = text6;
        this.sevenText = text7;
        this.eightText = text8;
        this.nineText = text9;
        invalidate();
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
        mTextPaint.setTextSize(textSize);
        invalidate();
    }

    public void setTextColor(int resId) {
        this.textColor = getResources().getColorStateList(resId);
        invalidate();
    }

    public void setStrokeColorColor(int startColor) {
        this.strokeColor = startColor;
    }

    public void setStrokeWidth(int strokeWidth) {
        this.strokeWidth = strokeWidth;
        paint.setStrokeWidth(strokeWidth);
    }

    public void setRadius(int radius) {
        this.radius = radius;
        invalidate();
    }

    public void setSelectedKey(int key, boolean isSelected) {
        Boolean selected = selectedKey.get(key);
        if (selected == null || !selected) {
            selected = true;
        } else {
            selected = false;
        }
        selectedKey.put(key, selected);
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        if (addWidth > 0) {
            width = width + addWidth * 2;
            height = height + addWidth * 2;
            setMeasuredDimension(width, height);
        }
        int edgeWidth = addWidth + strokeWidth;
        int centerHeight = height - edgeWidth * 2;
        int centerWidth = width - edgeWidth * 2;
        if(keyNum == 1){
            mPath1.reset();
            mPath1.moveTo(edgeWidth, edgeWidth + centerRadius);
            mPath1.lineTo(edgeWidth, height - edgeWidth - centerRadius);
            mPath1.quadTo(edgeWidth, height - edgeWidth, edgeWidth + centerRadius, height - edgeWidth);
            mPath1.lineTo(width - edgeWidth - centerRadius, height - edgeWidth);
            mPath1.quadTo(width - edgeWidth, height - edgeWidth, width - edgeWidth, height - edgeWidth - centerRadius);
            mPath1.lineTo(width - edgeWidth, edgeWidth + centerRadius);
            mPath1.quadTo(width - edgeWidth, edgeWidth, width - edgeWidth - centerRadius, edgeWidth);
            mPath1.lineTo(edgeWidth + centerRadius, edgeWidth);
            mPath1.quadTo(edgeWidth, edgeWidth, edgeWidth, edgeWidth + centerRadius);
            mPath1.close();
        } else if (keyNum == 2) {
            if(isPanel){
                mLineRectF1.left = (width - lineHeight) / 2f;
                mLineRectF1.top = edgeWidth;
                mLineRectF1.right = (width + lineHeight) / 2f;
                mLineRectF1.bottom = height - edgeWidth;

                mPath1.reset();
                mPath1.moveTo(mLineRectF1.left, mLineRectF1.top);
                mPath1.lineTo(mLineRectF1.left, mLineRectF1.bottom);
                mPath1.lineTo(edgeWidth + centerRadius, mLineRectF1.bottom);
                mPath1.quadTo(edgeWidth, mLineRectF1.bottom, edgeWidth, mLineRectF1.bottom - centerRadius);
                mPath1.lineTo(edgeWidth, mLineRectF1.top + centerRadius);
                mPath1.quadTo(edgeWidth, mLineRectF1.top, edgeWidth + centerRadius, mLineRectF1.top);
                mPath1.lineTo(mLineRectF1.left, mLineRectF1.top);
                mPath1.close();

                mPath2.reset();
                mPath2.moveTo(mLineRectF1.right, mLineRectF1.top);
                mPath2.lineTo(width - edgeWidth - centerRadius, mLineRectF1.top);
                mPath2.quadTo(width - edgeWidth, mLineRectF1.top, width - edgeWidth, mLineRectF1.top + centerRadius);
                mPath2.lineTo(width - edgeWidth, height - edgeWidth - centerRadius);
                mPath2.quadTo(width - edgeWidth, height - edgeWidth, width - edgeWidth - centerRadius, height - edgeWidth);
                mPath2.lineTo(mLineRectF1.right, height - edgeWidth);
                mPath2.lineTo(mLineRectF1.right, mLineRectF1.top);
                mPath2.close();
            }else{
                mLineRectF1.left = edgeWidth;
                mLineRectF1.top = (height - lineHeight) / 2f;
                mLineRectF1.right = width - edgeWidth;
                mLineRectF1.bottom = (height + lineHeight) / 2f;

                mPath1.reset();
                mPath1.moveTo(mLineRectF1.left, mLineRectF1.top);
                mPath1.lineTo(mLineRectF1.right, mLineRectF1.top);
                mPath1.lineTo(mLineRectF1.right, edgeWidth + centerRadius);
                mPath1.quadTo(mLineRectF1.right, edgeWidth, mLineRectF1.right - centerRadius, edgeWidth);
                mPath1.lineTo(mLineRectF1.left + centerRadius, edgeWidth);
                mPath1.quadTo(mLineRectF1.left, edgeWidth, mLineRectF1.left, edgeWidth + centerRadius);
                mPath1.lineTo(mLineRectF1.left, mLineRectF1.top);
                mPath1.close();

                mPath2.reset();
                mPath2.moveTo(mLineRectF1.left, mLineRectF1.bottom);
                mPath2.lineTo(mLineRectF1.right, mLineRectF1.bottom);
                mPath2.lineTo(mLineRectF1.right, height - edgeWidth - centerRadius);
                mPath2.quadTo(mLineRectF1.right, height - edgeWidth, mLineRectF1.right - centerRadius, height - edgeWidth);
                mPath2.lineTo(mLineRectF1.left + centerRadius, height - edgeWidth);
                mPath2.quadTo(mLineRectF1.left, height - edgeWidth, mLineRectF1.left, height - edgeWidth - centerRadius);
                mPath2.lineTo(mLineRectF1.left, mLineRectF1.bottom);
                mPath2.close();
            }
        } else if (keyNum == 3) {
            if(isPanel) {
                float perWidth = centerWidth / 3f;
                mLineRectF1.left = perWidth + edgeWidth - lineHeight / 2f;
                mLineRectF1.top = edgeWidth;
                mLineRectF1.right = mLineRectF1.left + lineHeight;
                mLineRectF1.bottom = height - edgeWidth;

                mLineRectF2.left = mLineRectF1.right + perWidth;
                mLineRectF2.top = edgeWidth;
                mLineRectF2.right = mLineRectF2.left + lineHeight;
                mLineRectF2.bottom = height - edgeWidth;

                mPath1.reset();
                mPath1.moveTo(mLineRectF1.left, mLineRectF1.top);
                mPath1.lineTo(mLineRectF1.left, mLineRectF1.bottom);
                mPath1.lineTo(edgeWidth + centerRadius, mLineRectF1.bottom);
                mPath1.quadTo(edgeWidth, mLineRectF1.bottom, edgeWidth, mLineRectF1.bottom - centerRadius);
                mPath1.lineTo(edgeWidth, mLineRectF1.top + centerRadius);
                mPath1.quadTo(edgeWidth, mLineRectF1.top, edgeWidth + centerRadius, mLineRectF1.top);
                mPath1.lineTo(mLineRectF1.left, mLineRectF1.top);
                mPath1.close();

                mPath2.reset();
                mPath2.moveTo(mLineRectF1.right, mLineRectF1.top);
                mPath2.lineTo(mLineRectF1.right + perWidth, mLineRectF1.top);
                mPath2.lineTo(mLineRectF1.right + perWidth, height - edgeWidth);
                mPath2.lineTo(mLineRectF1.right, height - edgeWidth);
                mPath2.lineTo(mLineRectF1.right, mLineRectF1.top);
                mPath2.close();

                mPath3.reset();
                mPath3.moveTo(mLineRectF2.right, mLineRectF2.top);
                mPath3.lineTo(width - edgeWidth - centerRadius, mLineRectF2.top);
                mPath3.quadTo(width - edgeWidth, mLineRectF2.top, width - edgeWidth, mLineRectF2.top + centerRadius);
                mPath3.lineTo(width - edgeWidth, height - edgeWidth - centerRadius);
                mPath3.quadTo(width - edgeWidth, height - edgeWidth, width - edgeWidth - centerRadius, height - edgeWidth);
                mPath3.lineTo(mLineRectF2.right, height - edgeWidth);
                mPath3.lineTo(mLineRectF2.right, mLineRectF2.top);
                mPath3.close();
            }else{
                float perHeight = centerHeight / 3f;
                mLineRectF1.left = edgeWidth;
                mLineRectF1.top = perHeight + edgeWidth - lineHeight / 2f;
                mLineRectF1.right = width - edgeWidth;
                mLineRectF1.bottom = mLineRectF1.top + lineHeight;

                mLineRectF2.left = edgeWidth;
                mLineRectF2.top = mLineRectF1.top + perHeight;
                mLineRectF2.right = width - edgeWidth;
                mLineRectF2.bottom = mLineRectF2.top + lineHeight;

                mPath1.reset();
                mPath1.moveTo(mLineRectF1.left, mLineRectF1.top);
                mPath1.lineTo(mLineRectF1.right, mLineRectF1.top);
                mPath1.lineTo(mLineRectF1.right, edgeWidth + centerRadius);
                mPath1.quadTo(mLineRectF1.right, edgeWidth, mLineRectF1.right - centerRadius, edgeWidth);
                mPath1.lineTo(mLineRectF1.left + centerRadius, edgeWidth);
                mPath1.quadTo(mLineRectF1.left, edgeWidth, mLineRectF1.left, edgeWidth + centerRadius);
                mPath1.lineTo(mLineRectF1.left, mLineRectF1.top);
                mPath1.close();

                mPath2.reset();
                mPath2.moveTo(mLineRectF1.left, mLineRectF1.bottom);
                mPath2.lineTo(mLineRectF1.right, mLineRectF1.bottom);
                mPath2.lineTo(mLineRectF1.right, mLineRectF1.bottom + perHeight - lineHeight);
                mPath2.lineTo(mLineRectF1.left, mLineRectF1.bottom + perHeight - lineHeight);
                mPath2.lineTo(mLineRectF1.left, mLineRectF1.bottom);
                mPath2.close();

                mPath3.reset();
                mPath3.moveTo(mLineRectF2.left, mLineRectF2.bottom);
                mPath3.lineTo(mLineRectF2.right, mLineRectF2.bottom);
                mPath3.lineTo(mLineRectF2.right, height - edgeWidth - centerRadius);
                mPath3.quadTo(mLineRectF2.right, height - edgeWidth, mLineRectF2.right - centerRadius, height - edgeWidth);
                mPath3.lineTo(mLineRectF2.left + centerRadius, height - edgeWidth);
                mPath3.quadTo(mLineRectF2.left, height - edgeWidth, mLineRectF2.left, height - edgeWidth - centerRadius);
                mPath3.lineTo(mLineRectF2.left, mLineRectF2.bottom);
                mPath3.close();
            }
        } else if (keyNum == 4) {
            mLineRectF1.left = edgeWidth;
            mLineRectF1.top = (height - lineHeight) / 2f;
            mLineRectF1.right = width - edgeWidth;
            mLineRectF1.bottom = (height + lineHeight) / 2f;

            mLineRectF2.left = (width - lineHeight) / 2f;
            mLineRectF2.top = edgeWidth;
            mLineRectF2.right = mLineRectF2.left + lineHeight;
            mLineRectF2.bottom = height - edgeWidth;

            mPath1.reset();
            mPath1.moveTo(mLineRectF1.left, mLineRectF1.top);
            mPath1.lineTo(mLineRectF2.left, mLineRectF1.top);
            mPath1.lineTo(mLineRectF2.left, edgeWidth);
            mPath1.lineTo(mLineRectF1.left + centerRadius, edgeWidth);
            mPath1.quadTo(mLineRectF1.left, edgeWidth, mLineRectF1.left, edgeWidth + centerRadius);
            mPath1.lineTo(mLineRectF1.left, mLineRectF1.top);
            mPath1.close();

            mPath2.reset();
            mPath2.moveTo(mLineRectF2.right, mLineRectF1.top);
            mPath2.lineTo(mLineRectF1.right, mLineRectF1.top);
            mPath2.lineTo(mLineRectF1.right, edgeWidth + centerRadius);
            mPath2.quadTo(mLineRectF1.right, edgeWidth, mLineRectF1.right - centerRadius, edgeWidth);
            mPath2.lineTo(mLineRectF2.right, edgeWidth);
            mPath2.lineTo(mLineRectF2.right, mLineRectF1.top);
            mPath2.close();

            mPath3.reset();
            mPath3.moveTo(mLineRectF1.left, mLineRectF1.bottom);
            mPath3.lineTo(mLineRectF2.left, mLineRectF1.bottom);
            mPath3.lineTo(mLineRectF2.left, height - edgeWidth);
            mPath3.lineTo(mLineRectF1.left + centerRadius, height - edgeWidth);
            mPath3.quadTo(mLineRectF1.left, height - edgeWidth, mLineRectF1.left, height - edgeWidth - centerRadius);
            mPath3.lineTo(mLineRectF1.left, mLineRectF1.bottom);
            mPath3.close();

            mPath4.reset();
            mPath4.moveTo(mLineRectF2.right, mLineRectF1.bottom);
            mPath4.lineTo(mLineRectF1.right, mLineRectF1.bottom);
            mPath4.lineTo(mLineRectF1.right, height - edgeWidth - centerRadius);
            mPath4.quadTo(mLineRectF1.right, height - edgeWidth, mLineRectF1.right - centerRadius, height - edgeWidth);
            mPath4.lineTo(mLineRectF2.right, height - edgeWidth);
            mPath4.lineTo(mLineRectF2.right, mLineRectF1.bottom);
            mPath4.close();
        } else if(keyNum == 6){
            float perHeight = centerHeight / 2f;
            float perWidth = centerWidth / 3f;
            //横线1
            mLineRectF1.left = edgeWidth;
            mLineRectF1.top = perHeight + edgeWidth - lineHeight / 2f;
            mLineRectF1.right = width - edgeWidth;
            mLineRectF1.bottom = mLineRectF1.top + lineHeight;

            //竖线1
            mLineRectF2.left = edgeWidth + perWidth - lineHeight / 2f;
            mLineRectF2.top = edgeWidth;
            mLineRectF2.right = mLineRectF2.left + lineHeight;
            mLineRectF2.bottom = height - edgeWidth;

            //竖线2
            mLineRectF3.left = mLineRectF2.left + perWidth;
            mLineRectF3.top = edgeWidth;
            mLineRectF3.right = mLineRectF3.left + lineHeight;
            mLineRectF3.bottom = height - edgeWidth;

            mPath1.reset();
            mPath1.moveTo(mLineRectF1.left, mLineRectF1.top);
            mPath1.lineTo(mLineRectF2.left, mLineRectF1.top);
            mPath1.lineTo(mLineRectF2.left, edgeWidth);
            mPath1.lineTo(mLineRectF1.left + centerRadius, edgeWidth);
            mPath1.quadTo(mLineRectF1.left, edgeWidth, mLineRectF1.left, edgeWidth + centerRadius);
            mPath1.lineTo(mLineRectF1.left, mLineRectF1.top);
            mPath1.close();

            mPath2.reset();
            mPath2.moveTo(mLineRectF2.right, mLineRectF1.top);
            mPath2.lineTo(mLineRectF3.left, mLineRectF1.top);
            mPath2.lineTo(mLineRectF3.left, edgeWidth);
            mPath2.lineTo(mLineRectF2.right, edgeWidth);
            mPath2.lineTo(mLineRectF2.right, mLineRectF1.top);
            mPath2.close();

            mPath3.reset();
            mPath3.moveTo(mLineRectF3.right, mLineRectF1.top);
            mPath3.lineTo(width - edgeWidth, mLineRectF1.top);
            mPath3.lineTo(width - edgeWidth, edgeWidth + centerRadius);
            mPath3.quadTo(width - edgeWidth, edgeWidth, width - edgeWidth - centerRadius, edgeWidth);
            mPath3.lineTo(mLineRectF3.right, edgeWidth);
            mPath3.lineTo(mLineRectF3.right, mLineRectF1.top);
            mPath3.close();

            mPath4.reset();
            mPath4.moveTo(mLineRectF1.left, mLineRectF1.bottom);
            mPath4.lineTo(mLineRectF2.left, mLineRectF1.bottom);
            mPath4.lineTo(mLineRectF2.left, height - edgeWidth);
            mPath4.lineTo(mLineRectF1.left + centerRadius, height - edgeWidth);
            mPath4.quadTo(mLineRectF1.left, height - edgeWidth, mLineRectF1.left, height - edgeWidth - centerRadius);
            mPath4.lineTo(mLineRectF1.left, mLineRectF1.bottom);
            mPath4.close();

            mPath5.reset();
            mPath5.moveTo(mLineRectF2.right, mLineRectF1.bottom);
            mPath5.lineTo(mLineRectF3.left, mLineRectF1.bottom);
            mPath5.lineTo(mLineRectF3.left, height - edgeWidth);
            mPath5.lineTo(mLineRectF2.right, height - edgeWidth);
            mPath5.lineTo(mLineRectF2.right, mLineRectF1.bottom);
            mPath5.close();

            mPath6.reset();
            mPath6.moveTo(mLineRectF3.right, mLineRectF1.bottom);
            mPath6.lineTo(width - edgeWidth, mLineRectF1.bottom);
            mPath6.lineTo(width - edgeWidth, height - edgeWidth - centerRadius);
            mPath6.quadTo(width - edgeWidth, height - edgeWidth, width - edgeWidth - centerRadius, height - edgeWidth);
            mPath6.lineTo(mLineRectF3.right, height - edgeWidth);
            mPath6.lineTo(mLineRectF3.right, mLineRectF1.bottom);
            mPath6.close();
        } else if (keyNum == 8) {
            float perHeight = (centerHeight - strokeWidth) / 4f;
            float perWidth = centerWidth / 2f;
            //横线1
            mLineRectF1.left = edgeWidth;
            mLineRectF1.top = perHeight + edgeWidth - lineHeight / 2f;
            mLineRectF1.right = width - edgeWidth;
            mLineRectF1.bottom = mLineRectF1.top + lineHeight;

            //横线2
            mLineRectF2.left = edgeWidth;
            mLineRectF2.top = mLineRectF1.top + perHeight * 2 + strokeWidth;
            mLineRectF2.right = width - edgeWidth;
            mLineRectF2.bottom = mLineRectF2.top + lineHeight;

            //竖线1
            mLineRectF3.left = edgeWidth + perWidth - lineHeight / 2f;
            mLineRectF3.top = edgeWidth;
            mLineRectF3.right = mLineRectF3.left + lineHeight;
            mLineRectF3.bottom = mLineRectF3.top + perHeight * 2;

            //竖线2
            mLineRectF4.left = mLineRectF3.left;
            mLineRectF4.top = edgeWidth + perHeight * 2 + strokeWidth;
            mLineRectF4.right = mLineRectF4.left + lineHeight;
            mLineRectF4.bottom = height - edgeWidth;

            float bottom = (height - strokeWidth) / 2f;
            mPath1.reset();
            mPath1.moveTo(mLineRectF1.left, mLineRectF1.top);
            mPath1.lineTo(mLineRectF3.left, mLineRectF1.top);
            mPath1.lineTo(mLineRectF3.left, edgeWidth);
            mPath1.lineTo(mLineRectF1.left + centerRadius, edgeWidth);
            mPath1.quadTo(mLineRectF1.left, edgeWidth, mLineRectF1.left, edgeWidth + centerRadius);
            mPath1.lineTo(mLineRectF1.left, mLineRectF1.top);
            mPath1.close();

            mPath2.reset();
            mPath2.moveTo(mLineRectF3.right, mLineRectF1.top);
            mPath2.lineTo(width - edgeWidth, mLineRectF1.top);
            mPath2.lineTo(width - edgeWidth, edgeWidth + centerRadius);
            mPath2.quadTo(width - edgeWidth, edgeWidth, width - edgeWidth - centerRadius, edgeWidth);
            mPath2.lineTo(mLineRectF3.right, edgeWidth);
            mPath2.lineTo(mLineRectF3.right, mLineRectF1.top);
            mPath2.close();

            mPath3.reset();
            mPath3.moveTo(mLineRectF1.left, mLineRectF1.bottom);
            mPath3.lineTo(mLineRectF3.left, mLineRectF1.bottom);
            mPath3.lineTo(mLineRectF3.left, bottom);
            mPath3.lineTo(mLineRectF1.left + centerRadius, bottom);
            mPath3.quadTo(mLineRectF1.left, bottom, mLineRectF1.left, bottom - centerRadius);
            mPath3.lineTo(mLineRectF1.left, mLineRectF1.bottom);
            mPath3.close();

            mPath4.reset();
            mPath4.moveTo(mLineRectF3.right, mLineRectF1.bottom);
            mPath4.lineTo(width - edgeWidth, mLineRectF1.bottom);
            mPath4.lineTo(width - edgeWidth, bottom - centerRadius);
            mPath4.quadTo(width - edgeWidth, bottom, width - edgeWidth - centerRadius, bottom);
            mPath4.lineTo(mLineRectF3.right, bottom);
            mPath4.lineTo(mLineRectF3.right, mLineRectF1.bottom);
            mPath4.close();

            float top = (height + strokeWidth) / 2f;
            mPath5.reset();
            mPath5.moveTo(mLineRectF2.left, mLineRectF2.top);
            mPath5.lineTo(mLineRectF4.left, mLineRectF2.top);
            mPath5.lineTo(mLineRectF4.left, top);
            mPath5.lineTo(mLineRectF2.left + centerRadius, top);
            mPath5.quadTo(mLineRectF2.left, top, mLineRectF2.left, top + centerRadius);
            mPath5.lineTo(mLineRectF2.left, mLineRectF2.top);
            mPath5.close();

            mPath6.reset();
            mPath6.moveTo(mLineRectF4.right, mLineRectF2.top);
            mPath6.lineTo(width - edgeWidth, mLineRectF2.top);
            mPath6.lineTo(width - edgeWidth, top + centerRadius);
            mPath6.quadTo(width - edgeWidth, top, width - edgeWidth - centerRadius, top);
            mPath6.lineTo(mLineRectF4.right, top);
            mPath6.lineTo(mLineRectF4.right, mLineRectF2.top);
            mPath6.close();

            mPath7.reset();
            mPath7.moveTo(mLineRectF2.left, mLineRectF2.bottom);
            mPath7.lineTo(mLineRectF4.left, mLineRectF2.bottom);
            mPath7.lineTo(mLineRectF4.left, height - edgeWidth);
            mPath7.lineTo(mLineRectF2.left + centerRadius, height - edgeWidth);
            mPath7.quadTo(mLineRectF2.left, height - edgeWidth, mLineRectF2.left, height - edgeWidth - centerRadius);
            mPath7.lineTo(mLineRectF2.left, mLineRectF2.bottom);
            mPath7.close();

            mPath8.reset();
            mPath8.moveTo(mLineRectF4.right, mLineRectF2.bottom);
            mPath8.lineTo(width - edgeWidth, mLineRectF2.bottom);
            mPath8.lineTo(width - edgeWidth, height - edgeWidth - centerRadius);
            mPath8.quadTo(width - edgeWidth, height - edgeWidth, width - edgeWidth - centerRadius, height - edgeWidth);
            mPath8.lineTo(mLineRectF4.right, height - edgeWidth);
            mPath8.lineTo(mLineRectF4.right, mLineRectF2.bottom);
            mPath8.close();
        } else if (keyNum == 9) {
            float perHeight = centerHeight / 3f;
            float perWidth = centerWidth / 3f;
            //横线1
            mLineRectF1.left = edgeWidth;
            mLineRectF1.top = perHeight + edgeWidth - lineHeight / 2f;
            mLineRectF1.right = width - edgeWidth;
            mLineRectF1.bottom = mLineRectF1.top + lineHeight;

            //横线2
            mLineRectF2.left = edgeWidth;
            mLineRectF2.top = mLineRectF1.top + perHeight;
            mLineRectF2.right = width - edgeWidth;
            mLineRectF2.bottom = mLineRectF2.top + lineHeight;

            //竖线1
            mLineRectF3.left = edgeWidth + perWidth - lineHeight / 2f;
            mLineRectF3.top = edgeWidth;
            mLineRectF3.right = mLineRectF3.left + lineHeight;
            mLineRectF3.bottom = height - edgeWidth;

            //竖线2
            mLineRectF4.left = mLineRectF3.left + perWidth;
            mLineRectF4.top = edgeWidth;
            mLineRectF4.right = mLineRectF4.left + lineHeight;
            mLineRectF4.bottom = height - edgeWidth;

            mPath1.reset();
            mPath1.moveTo(mLineRectF1.left, mLineRectF1.top);
            mPath1.lineTo(mLineRectF3.left, mLineRectF1.top);
            mPath1.lineTo(mLineRectF3.left, edgeWidth);
            mPath1.lineTo(mLineRectF1.left + centerRadius, edgeWidth);
            mPath1.quadTo(mLineRectF1.left, edgeWidth, mLineRectF1.left, edgeWidth + centerRadius);
            mPath1.lineTo(mLineRectF1.left, mLineRectF1.top);
            mPath1.close();

            mPath2.reset();
            mPath2.moveTo(mLineRectF3.right, mLineRectF1.top);
            mPath2.lineTo(mLineRectF4.left, mLineRectF1.top);
            mPath2.lineTo(mLineRectF4.left, edgeWidth);
            mPath2.lineTo(mLineRectF3.right, edgeWidth);
            mPath2.lineTo(mLineRectF3.right, mLineRectF1.top);
            mPath2.close();

            mPath3.reset();
            mPath3.moveTo(mLineRectF4.right, mLineRectF1.top);
            mPath3.lineTo(width - edgeWidth, mLineRectF1.top);
            mPath3.lineTo(width - edgeWidth, edgeWidth + centerRadius);
            mPath3.quadTo(width - edgeWidth, edgeWidth, width - edgeWidth - centerRadius, edgeWidth);
            mPath3.lineTo(mLineRectF4.right, edgeWidth);
            mPath3.lineTo(mLineRectF4.right, mLineRectF1.top);
            mPath3.close();

            mPath4.reset();
            mPath4.moveTo(mLineRectF1.left, mLineRectF1.bottom);
            mPath4.lineTo(mLineRectF3.left, mLineRectF1.bottom);
            mPath4.lineTo(mLineRectF3.left, mLineRectF1.bottom + perHeight - lineHeight);
            mPath4.lineTo(mLineRectF1.left, mLineRectF1.bottom + perHeight - lineHeight);
            mPath4.lineTo(mLineRectF1.left, mLineRectF1.bottom);
            mPath4.close();

            mPath5.reset();
            mPath5.moveTo(mLineRectF3.right, mLineRectF1.bottom);
            mPath5.lineTo(mLineRectF4.left, mLineRectF1.bottom);
            mPath5.lineTo(mLineRectF4.left, mLineRectF1.bottom + perHeight - lineHeight);
            mPath5.lineTo(mLineRectF3.right, mLineRectF1.bottom + perHeight - lineHeight);
            mPath5.lineTo(mLineRectF3.right, mLineRectF1.bottom);
            mPath5.close();

            mPath6.reset();
            mPath6.moveTo(mLineRectF4.right, mLineRectF1.bottom);
            mPath6.lineTo(width - edgeWidth, mLineRectF1.bottom);
            mPath6.lineTo(width - edgeWidth, mLineRectF1.bottom + perHeight - lineHeight);
            mPath6.lineTo(mLineRectF4.right, mLineRectF1.bottom + perHeight - lineHeight);
            mPath6.lineTo(mLineRectF4.right, mLineRectF1.bottom);
            mPath6.close();

            mPath7.reset();
            mPath7.moveTo(mLineRectF2.left, mLineRectF2.bottom);
            mPath7.lineTo(mLineRectF3.left, mLineRectF2.bottom);
            mPath7.lineTo(mLineRectF3.left, height - edgeWidth);
            mPath7.lineTo(mLineRectF2.left + centerRadius, height - edgeWidth);
            mPath7.quadTo(mLineRectF2.left, height - edgeWidth, mLineRectF2.left, height - edgeWidth - centerRadius);
            mPath7.lineTo(mLineRectF2.left, mLineRectF2.bottom);
            mPath7.close();

            mPath8.reset();
            mPath8.moveTo(mLineRectF3.right, mLineRectF2.bottom);
            mPath8.lineTo(mLineRectF4.left, mLineRectF2.bottom);
            mPath8.lineTo(mLineRectF4.left, mLineRectF2.bottom + perHeight - lineHeight);
            mPath8.lineTo(mLineRectF3.right, mLineRectF2.bottom + perHeight - lineHeight);
            mPath8.lineTo(mLineRectF3.right, mLineRectF2.bottom);
            mPath8.close();

            mPath9.reset();
            mPath9.moveTo(mLineRectF4.right, mLineRectF2.bottom);
            mPath9.lineTo(width - edgeWidth, mLineRectF2.bottom);
            mPath9.lineTo(width - edgeWidth, height - edgeWidth - centerRadius);
            mPath9.quadTo(width - edgeWidth, height - edgeWidth, width - edgeWidth - centerRadius, height - edgeWidth);
            mPath9.lineTo(mLineRectF4.right, height - edgeWidth);
            mPath9.lineTo(mLineRectF4.right, mLineRectF2.bottom);
            mPath9.close();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        paint.setStyle(Paint.Style.FILL);
        drawDividingLine(canvas);
        paint.setColor(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        switch (getPressKeyPosition()) {
            case 1:
                canvas.drawPath(mPath1, paint);
                break;
            case 2:
                canvas.drawPath(mPath2, paint);
                break;
            case 3:
                canvas.drawPath(mPath3, paint);
                break;
            case 4:
                canvas.drawPath(mPath4, paint);
                break;
            case 5:
                canvas.drawPath(mPath5, paint);
                break;
            case 6:
                canvas.drawPath(mPath6, paint);
                break;
            case 7:
                canvas.drawPath(mPath7, paint);
                break;
            case 8:
                canvas.drawPath(mPath8, paint);
                break;
            case 9:
                canvas.drawPath(mPath9, paint);
                break;
        }
        if (isEnabled()) {
            mTextPaint.setAlpha(255);
        } else {
            mTextPaint.setAlpha(80);
        }
        Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt();
        float textHeight = fontMetricsInt.descent - fontMetricsInt.ascent;
        int edgeWidth = addWidth + strokeWidth;
        float centerViewHeight = getHeight() - edgeWidth * 2;
        float centerViewWidth = getWidth() - edgeWidth * 2;
        //每个按键的高度
        float perHeight = 0;
        if(keyNum == 1){
            perHeight = centerViewHeight;
        } else if(keyNum == 2){
            if(isPanel){
                perHeight = centerViewHeight;
            }else{
                perHeight = centerViewHeight / 2f;
            }
        } else if(keyNum == 3){
            if(isPanel){
                perHeight = centerViewHeight;
            }else{
                perHeight = centerViewHeight / 3f;
            }
        } else if(keyNum == 4 || keyNum == 6){
            perHeight = centerViewHeight / 2f;
        } else if(keyNum == 9) {
            perHeight = centerViewHeight / 3f;
        }
        //每个按键的宽度
        float perWidth = 0;
        if(keyNum == 1){
            perWidth = centerViewWidth;
        } else if(keyNum == 2){
            if(isPanel){
                perWidth = centerViewWidth / 2f;
            }else{
                perWidth = centerViewWidth;
            }
        } else if (keyNum == 3) {
            if(isPanel){
                perWidth = centerViewWidth / 3f;
            }else{
                perWidth = centerViewWidth;
            }
        } else if (keyNum == 4) {
            perWidth = centerViewWidth / 2f;
        } else if(keyNum == 6){
            perWidth = centerViewHeight / 3f;
        }else if (keyNum == 8) {
            perWidth = centerViewWidth / 2f;
            perHeight = (centerViewHeight - strokeWidth) / 4f;
        } else if (keyNum == 9) {
            perWidth = centerViewWidth / 3f;
        }
        if (keyNum == 1) {
            drawText(canvas, 1, 0, 0, oneText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
        } else if (keyNum == 2) {
            if(isPanel){
                drawText(canvas, 1, 0, 0, oneText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
                drawText(canvas, 2, 1, 0, twoText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            }else{
                drawText(canvas, 1, 0, 0, oneText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
                drawText(canvas, 2, 0, 1, twoText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            }
        } else if (keyNum == 3) {
            if(isPanel){
                drawText(canvas, 1, 0, 0, oneText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
                drawText(canvas, 2, 1, 0, twoText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
                drawText(canvas, 3, 2, 0, threeText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            }else{
                drawText(canvas, 1, 0, 0, oneText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
                drawText(canvas, 2, 0, 1, twoText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
                drawText(canvas, 3, 0, 2, threeText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            }
        } else if (keyNum == 4) {
            drawText(canvas, 1, 0, 0, oneText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 2, 1, 0, twoText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 3, 0, 1, threeText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 4, 1, 1, fourText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
        }  else if (keyNum == 6) {
            drawText(canvas, 1, 0, 0, oneText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 2, 1, 0, twoText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 3, 2, 0, threeText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 4, 0, 1, fourText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 5, 1, 1, fiveText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 6, 2, 1, sixText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
        }  else if (keyNum == 8) {
            drawText(canvas, 1, 0, 0, oneText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 2, 1, 0, twoText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 3, 0, 1, threeText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 4, 1, 1, fourText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 5, 0, 2, fiveText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 6, 1, 2, sixText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 7, 0, 3, sevenText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 8, 1, 3, eightText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
        } else if (keyNum == 9) {
            drawText(canvas, 1, 0, 0, oneText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 2, 1, 0, twoText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 3, 2, 0, threeText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 4, 0, 1, fourText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 5, 1, 1, fiveText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 6, 2, 1, sixText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 7, 0, 2, sevenText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 8, 1, 2, eightText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
            drawText(canvas, 9, 2, 2, nineText, perWidth, perHeight, edgeWidth, textHeight, fontMetricsInt);
        }
    }

    private void drawText(Canvas canvas, int key, int positionX, int positionY, String text, float perWidth, float perHeight, int edgeWidth, float textHeight, Paint.FontMetricsInt fontMetricsInt) {
        String subtext = "";
        if (text.contains("sub")) {
            String[] parts = text.split("sub"); // 如果包含 "sub"，则进行分割
            text = parts[0];  // 获取 "sub" 前面的部分
            subtext = parts[1];  // 获取 "sub" 后面的部分
        }

        // 主文本宽度计算
        float textWidth = mTextPaint.measureText(text);
        String info = text;
        if (perWidth < textWidth) {
            // 超过宽度，显示省略号
            info = TextUtils.ellipsize(text, mTextPaint, perWidth, TextUtils.TruncateAt.END).toString();
            textWidth = mTextPaint.measureText(info);
        }

        // 子文本处理（多行换行）
        List<String> subtextLines = new ArrayList<>();
        if (!TextUtils.isEmpty(subtext)) {
            subtextLines = wrapText(subtext, perWidth, mTextPaint);
        }

        // 计算主文本和子文本的总高度
        int subtextLinesCount = subtextLines.size();
        float lineHeight = fontMetricsInt.descent - fontMetricsInt.ascent;
        float totalTextHeight = textHeight; // 主文本高度
        if (subtextLinesCount > 0) {
            totalTextHeight += lineHeight * subtextLinesCount; // 包括所有子文本行的高度
        }

        // 中心点计算
        float centerY = edgeWidth + (perHeight * positionY) + perHeight / 2f;

        // 主文本的 Y 坐标（从中心向上偏移一半总高度）
        float yText = centerY - totalTextHeight / 2f + textHeight / 2f - fontMetricsInt.descent;

        // 设置文本颜色
        Boolean selected = selectedKey.get(key);
        if (getPressKeyPosition() == key) {
            mTextPaint.setColor(0xffffffff); // 按下时的颜色
        } else {
            mTextPaint.setColor(textColor.getColorForState(selected != null && selected ? SELECTED_STATE_SET : UNSELECTED_STATE_SET, textColor.getDefaultColor()));
        }

        // 绘制主文本
        canvas.drawText(info, edgeWidth + (perWidth * positionX) + (perWidth - textWidth) / 2f, yText, mTextPaint);

        // 如果有子文本，则绘制子文本
        if (!subtextLines.isEmpty()) {
            // 设置子文本颜色和大小
            Paint originalPaint = new Paint(mTextPaint); // 保存原始的 Paint 配置
            mTextPaint.setColor(0xFF989FAD); // 子文本颜色
            mTextPaint.setTextSize(subTextSize); // 子文本字体大小

            // 子文本起始 Y 坐标（主文本底部）
            float ySubtext = yText + textHeight;

            for (String line : subtextLines) {
                float subtextWidth = mTextPaint.measureText(line);
                String subInfo = line;
                if (perWidth < subtextWidth) {
                    // 超过宽度，显示省略号
                    subInfo = TextUtils.ellipsize(line, mTextPaint, perWidth, TextUtils.TruncateAt.END).toString();
                }

                // 绘制子文本
                canvas.drawText(subInfo, edgeWidth + (perWidth * positionX) + (perWidth - subtextWidth) / 2f, ySubtext, mTextPaint);

                // 更新 Y 坐标，下一行文本距离上一行的高度
                ySubtext += lineHeight;
            }

            // 恢复原始 Paint 配置
            mTextPaint.set(originalPaint);
        }
    }


    // 用于分割文本的工具方法
    private List<String> wrapText(String text, float maxWidth, Paint paint) {
        List<String> lines = new ArrayList<>();
        int start = 0;
        while (start < text.length()) {
            int end = paint.breakText(text, start, text.length(), true, maxWidth, null);
            lines.add(text.substring(start, start + end));
            start += end;
        }
        return lines;
    }


    private void drawDividingLine(Canvas canvas) {
        if (keyNum == 8) {
            mRectF.left = addWidth;
            mRectF.top = addWidth;
            mRectF.right = getWidth() - addWidth;
            mRectF.bottom = getHeight() - addWidth;
            paint.setColor(strokeColor);
            paint.clearShadowLayer();
            paint.setStyle(Paint.Style.FILL);
            canvas.drawRoundRect(mRectF, radius, radius, paint);

            mRectF.left = addWidth + strokeWidth;
            mRectF.top = addWidth + strokeWidth;
            mRectF.right = getWidth() - addWidth - strokeWidth;
            mRectF.bottom = (getHeight() - strokeWidth) / 2f;
            paint.setColor(deviceSolidBgColor);
            paint.setShadowLayer(shadowRadius1, 0, shadowRadiusDy1, 0x0825334b);
            canvas.drawRoundRect(mRectF, centerRadius, centerRadius, paint);

            paint.setShadowLayer(shadowRadius2, 0, shadowRadiusDy2, 0x1025334b);
            canvas.drawRoundRect(mRectF, centerRadius, centerRadius, paint);

            paint.setShadowLayer(shadowRadius3, 0, shadowRadiusDy3, 0x0225334b);
            canvas.drawRoundRect(mRectF, centerRadius, centerRadius, paint);

            mRectF.left = addWidth + strokeWidth;
            mRectF.top = (getHeight() + strokeWidth) / 2f;
            mRectF.right = getWidth() - addWidth - strokeWidth;
            mRectF.bottom = getHeight() - addWidth - strokeWidth;

            paint.setShadowLayer(shadowRadius1, 0, shadowRadiusDy1, 0x0825334b);
            canvas.drawRoundRect(mRectF, centerRadius, centerRadius, paint);

            paint.setShadowLayer(shadowRadius2, 0, shadowRadiusDy2, 0x1025334b);
            canvas.drawRoundRect(mRectF, centerRadius, centerRadius, paint);

            paint.setShadowLayer(shadowRadius3, 0, shadowRadiusDy3, 0x0225334b);
            canvas.drawRoundRect(mRectF, centerRadius, centerRadius, paint);
        } else {
            float strokeHalfWidth = strokeWidth / 2f;
            mRectF.left = addWidth + strokeHalfWidth;
            mRectF.top = addWidth + strokeHalfWidth;
            mRectF.right = getWidth() - addWidth - strokeHalfWidth;
            mRectF.bottom = getHeight() - addWidth - strokeHalfWidth;
            paint.setColor(strokeColor);
            paint.clearShadowLayer();
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(strokeWidth);
            canvas.drawRoundRect(mRectF, radius, radius, paint);

            paint.setStyle(Paint.Style.FILL);
            paint.setStrokeWidth(0);
            paint.setColor(deviceSolidBgColor);
            mRectF.left = addWidth + strokeWidth;
            mRectF.top = addWidth + strokeWidth;
            mRectF.right = getWidth() - addWidth - strokeWidth;
            mRectF.bottom = getHeight() - addWidth - strokeWidth;

            paint.setShadowLayer(shadowRadius1, 0, shadowRadiusDy1, 0x0825334b);
            canvas.drawRoundRect(mRectF, centerRadius, centerRadius, paint);

            paint.setShadowLayer(shadowRadius2, 0, shadowRadiusDy2, 0x1025334b);
            canvas.drawRoundRect(mRectF, centerRadius, centerRadius, paint);

            paint.setShadowLayer(shadowRadius3, 0, shadowRadiusDy3, 0x0225334b);
            canvas.drawRoundRect(mRectF, centerRadius, centerRadius, paint);
        }
        paint.clearShadowLayer();
        paint.setColor(lineColor);
        if(mLineRectF1 != null){
            canvas.drawRect(mLineRectF1, paint);
        }
        if (mLineRectF2 != null) {
            canvas.drawRect(mLineRectF2, paint);
        }
        if (mLineRectF3 != null) {
            canvas.drawRect(mLineRectF3, paint);
        }
        if (mLineRectF4 != null) {
            canvas.drawRect(mLineRectF4, paint);
        }
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        invalidate();
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        invalidate();
    }

    @Override
    public int clickPosition(int x, int y) {
        RectF bounds = new RectF();
        Region region = new Region();
        Region clickRegion = new Region();
        switch (keyNum) {
            case 1:
                mPath1.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath1, region);
                if (clickRegion.contains(x, y)) {
                    return 1;
                }
                return 0;
            case 2:
                mPath1.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath1, region);
                if (clickRegion.contains(x, y)) {
                    return 1;
                }
                mPath2.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath2, region);
                if (clickRegion.contains(x, y)) {
                    return 2;
                }
                return 0;
            case 3:
                mPath1.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath1, region);
                if (clickRegion.contains(x, y)) {
                    return 1;
                }
                mPath2.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath2, region);
                if (clickRegion.contains(x, y)) {
                    return 2;
                }
                mPath3.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath3, region);
                if (clickRegion.contains(x, y)) {
                    return 3;
                }
                return 0;
            case 4:
                mPath1.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath1, region);
                if (clickRegion.contains(x, y)) {
                    return 1;
                }
                mPath2.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath2, region);
                if (clickRegion.contains(x, y)) {
                    return 2;
                }
                mPath3.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath3, region);
                if (clickRegion.contains(x, y)) {
                    return 3;
                }
                mPath4.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath4, region);
                if (clickRegion.contains(x, y)) {
                    return 4;
                }
                return 0;
            case 6:
                mPath1.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath1, region);
                if (clickRegion.contains(x, y)) {
                    return 1;
                }
                mPath2.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath2, region);
                if (clickRegion.contains(x, y)) {
                    return 2;
                }
                mPath3.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath3, region);
                if (clickRegion.contains(x, y)) {
                    return 3;
                }
                mPath4.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath4, region);
                if (clickRegion.contains(x, y)) {
                    return 4;
                }
                mPath5.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath5, region);
                if (clickRegion.contains(x, y)) {
                    return 5;
                }
                mPath6.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath6, region);
                if (clickRegion.contains(x, y)) {
                    return 6;
                }
                return 0;
            case 8:
                mPath1.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath1, region);
                if (clickRegion.contains(x, y)) {
                    return 1;
                }
                mPath2.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath2, region);
                if (clickRegion.contains(x, y)) {
                    return 2;
                }
                mPath3.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath3, region);
                if (clickRegion.contains(x, y)) {
                    return 3;
                }
                mPath4.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath4, region);
                if (clickRegion.contains(x, y)) {
                    return 4;
                }
                mPath5.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath5, region);
                if (clickRegion.contains(x, y)) {
                    return 5;
                }
                mPath6.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath6, region);
                if (clickRegion.contains(x, y)) {
                    return 6;
                }
                mPath7.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath7, region);
                if (clickRegion.contains(x, y)) {
                    return 7;
                }
                mPath8.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath8, region);
                if (clickRegion.contains(x, y)) {
                    return 8;
                }
                return 0;
            case 9:
                mPath1.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath1, region);
                if (clickRegion.contains(x, y)) {
                    return 1;
                }
                mPath2.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath2, region);
                if (clickRegion.contains(x, y)) {
                    return 2;
                }
                mPath3.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath3, region);
                if (clickRegion.contains(x, y)) {
                    return 3;
                }
                mPath4.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath4, region);
                if (clickRegion.contains(x, y)) {
                    return 4;
                }
                mPath5.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath5, region);
                if (clickRegion.contains(x, y)) {
                    return 5;
                }
                mPath6.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath6, region);
                if (clickRegion.contains(x, y)) {
                    return 6;
                }
                mPath7.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath7, region);
                if (clickRegion.contains(x, y)) {
                    return 7;
                }
                mPath8.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath8, region);
                if (clickRegion.contains(x, y)) {
                    return 8;
                }
                mPath9.computeBounds(bounds, true);
                region.set((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom);
                clickRegion.setPath(mPath9, region);
                if (clickRegion.contains(x, y)) {
                    return 9;
                }
                return 0;
        }
        return 0;
    }
}
